<template>
    <div>
        <Header></Header>
        <div class="page-wrap w">
            <div class="panel">
                <h1 class="panel-title">收货地址</h1>
                <div v-if="showAddShipping">
                    <ShippingModel @saveShipping="doSaveShipping" @closeShippingModel="doUpdateShippingModel" :operation="shippingOperation" :shipping="updateShipping"></ShippingModel>
                </div>
                <div class="panel-body address-con">
                    <div v-for="(item,i) in shippingList">
                        <div :class="item.defaultShipping ? 'address-item active' : 'address-item'">
                            <div class="address-title">
                                {{item.receiverCity}} {{item.receiverProvince}} （ {{item.receiverName}} 收 ）
                            </div>
                            <div class="address-detail">
                                {{item.receiverAddress}} {{item.receiverPhone}}
                            </div>
                            <div class="address-opera">
                                <span class="link address-update" @click="doUpdateDefaultShipping(item.id)">设为默认的收货地址</span>
                                <span class="link address-update" @click="doUpdateShipping(item.id)">编辑</span>
                                <span class="link address-delete" @click="doDeleteShipping(item.id)">删除</span>
                            </div>
                        </div>
                    </div>
                    <div class="address-add">
                        <div class="address-new" @click="doUpdateShippingModel">
                            <i class="fa fa-plus"></i>
                            <div class="text">使用新地址</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel">
                <h1 class="panel-title">商品清单</h1>
                <div class="panel-body product-con">
                    <table class="product-table">
                        <tr>
                            <th class="cell-img">&nbsp;</th>
                            <th class="cell-info">商品描述</th>
                            <th class="cell-price">价格</th>
                            <th class="cell-count">数量</th>
                            <th class="cell-total">小计</th>
                        </tr>
                        <tr v-for="(item,i) in orderDto.orderItemDtoList">
                            <td class="cell-img">
                                <a v-bind:href="'/detail.html?productId=' + item.productId" target="_blank">
                                    <img class="p-img" v-bind:src="item.productImage" :alt="item.productName"/>
                                </a>
                            </td>
                            <td class="cell-info">
                                <a class="link" v-bind:href="'/detail.html?productId=' + item.productId" target="_blank">{{item.productName}}</a>
                            </td>
                            <td class="cell-price">￥{{item.currentUnitPrice}}</td>
                            <td class="cell-count">{{item.quantity}}</td>
                            <td class="cell-total">￥{{item.totalPrice}}</td>
                        </tr>
                    </table>
                    <div class="submit-con">
                        <span>订单总价:</span>
                        <span class="submit-total">￥{{orderDto.payment}}</span>
                        <span class="btn order-submit" @click="doCreateOrder">提交订单</span>
                    </div>
                </div>
            </div>
        </div>
        <Footer></Footer>
    </div>
</template>

<script>
import Header from "@/views/layout/header.vue";
import Footer from "@/views/layout/footer.vue";
import ShippingModel from "@/views/shipping/index.vue";
import {listShipping, createOrder, cancelOrder, getDetailByOrderNo, updateShipping, getShippingById, updateDefaultShipping, addShipping, getCurrentOrder, delShippingById} from '@/api/commonApi'
export default {
    name: "orderConfirm",
    components: {Header, Footer, ShippingModel},
    data: function() {
      return {
        isLoading: false,
        orderDto: {
            orderItemDtoList: []
        },
        shippingId: "",
        activeAddressId: "",
        shippingList: [],
        showAddShipping: false,
        shippingOperation: "add",
        updateShipping: {}
      }
    },
    mounted: function(){
        let _this = this;
        _this.doGetCurrentOrder()
        _this.doListShipping();
    },
    methods: {
        doGetCurrentOrder(){
            getCurrentOrder().then((res) => {
                if(res.data.status === 200){
                    this.orderDto = res.data.data
                }
            })
        },
        doCreateOrder(){
            createOrder({"shippingId": this.shippingId}).then((res) => {
                if(res.data.status === 200){
                    console.log(res.data)
                }
            })
        },
        doListShipping(){
            listShipping({"pageNum": 1, "pageSize": 10}).then((res) => {
                if(res.data.status === 200){
                    this.shippingList = res.data.data.list
                }
            })
        },
        doUpdateShippingModel(){
            this.showAddShipping = !this.showAddShipping
        },
        doSaveShipping(shipping){
            if(shipping && shipping.id){
                updateShipping(shipping).then(res => {
                    if(res.data.status === 200){
                        Toast.success("修改收货地址成功")
                        this.doUpdateShippingModel()
                        this.doListShipping()
                    }
                })
            } else {
                addShipping(shipping).then(res => {
                    if(res.data.status === 200){
                        Toast.success("新增收货地址成功")
                        this.doUpdateShippingModel()
                        this.doListShipping()
                    }
                })
            }
        },
        doUpdateDefaultShipping(id){
            let _this = this;
            Confirm.show('确认要设置该收获地址为默认收货地址吗？', function(){
                Loading.show();
                updateDefaultShipping(id).then(res => {    
                    Loading.hide();
                    if(res.data.status === 200){
                        _this.doListShipping()
                    }
                })
            })
        },
        doUpdateShipping(id){
            getShippingById({"id": id}).then(res => {
                if(res.data.status === 200){
                    this.updateShipping = res.data.data
                    this.showAddShipping = true
                }
            })
        },
        doDeleteShipping(id){
            let _this = this;
            Confirm.show('确认要删除该收获地址吗？', function(){
                Loading.show();
                delShippingById({"id": id}).then(res => {    
                    Loading.hide();
                    if(res.data.status === 200){
                        _this.doListShipping()
                    }
                })
            })
        }
    }
}
</script>

<style scoped>

/* 地址部分的样式 */
.address-item,
.address-add{
    margin:20px 20px 0 0;
    width: 200px;
    padding: 5px 8px;
    float: left;
    border: 3px dashed #ccc;
    cursor: pointer;
}
.address-item.active{
    border: 3px dashed #c60023;
}
.address-item:hover,
.address-add:hover{
    background: #fafafa;
}
.address-item .address-title{
    color: #666;
    height: 20px;
    line-height: 20px;
    overflow: hidden;
    border-bottom: 1px solid #ddd;
}
.address-item .address-detail{
    margin-top: 5px;
    height: 40px;
    line-height: 20px;
    overflow: hidden;
    color: #666;
}
.address-item .address-opera{
    text-align: right;
    visibility: hidden;
}
.address-item:hover .address-opera{
    visibility: visible;
}
.address-item .address-opera .link{
    margin-right: 5px;
}
.address-add .address-new{
    padding: 8px 0;
    text-align: center;
    color: #ccc;
}
.address-add .address-new .fa{
    font-size: 50px;
    line-height: 50px;
}
.address-add .address-new .text{
    font-size: 12px;
}

/* 商品清单部分 */
.product-table{
    width: 100%;
    margin: 10px 0;
    border-collapse: collapse;
}
.product-table th,
.product-table td{
    border-bottom: 1px dotted #ddd;
}
.product-table .cell-img{
    padding: 10px;
    width: 10%;
    text-align: center;
}
.product-table .cell-img .p-img{
    width: 80px;
    height: 80px;
    border: 1px solid #ddd;
}
.product-table .cell-info{
    width: 30%;
    text-align: left;
}
.product-table .cell-price{
    width: 20%;
    text-align: center;
}
.product-table .cell-count{
    width: 20%;
    text-align: center;
}
.product-table .cell-total{
    width: 20%;
    text-align: center;
}
/* 订单提交 */
.submit-con{
    background: #eee;
    text-align: right;
}
.submit-con .submit-total{
    margin-right: 20px;
    font-size: 18px;
    color: #c60023;
    font-weight: bold;
    vertical-align: middle;
}
.submit-con .order-submit{
    height: 50px;
    line-height: 50px;
    width: 100px;
    text-align: center;
}

/* modal部分 */
.modal{
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: #eee;
    background: rgba(0,0,0,.4);
    z-index: 1000;
    overflow-x: hidden;
    overflow-y: auto;
}
.modal .modal-container{
    width: 700px;
    margin: 50px auto;
    background: #fff;
    border-radius: 2px;
}
.modal .modal-container .modal-header{
    position: relative;
    padding: 15px;
    border-bottom: 1px solid #ccc;
}
.modal .modal-container .modal-header .modal-title{
    font-size: 14px;
}
.modal .modal-container .modal-header .close{
    position: absolute;
    padding: 10px;
    right: 10px;
    top: 10px;
    cursor: pointer;
}
.modal .modal-container .modal-body{
    padding: 15px;
}

/* form部分 */
.form .form-line{
    margin: 5px 0;
    overflow: hidden;
}
.form .form-line .label{
    display: block;
    float: left;
    width: 85px;
    text-align: right;
    height: 30px;
    line-height: 30px;
    margin-right: 10px;
    cursor: pointer;
}
.form .form-line .label .required{
    color: red;
}
.form .form-line .form-item{
    padding: 0 5px;
    height: 30px;
    line-height: 30px;
    outline: none;
    margin-right: 10px;
}
.form .form-line .btn{
    margin-left: 95px;
    height: 30px;
    line-height: 30px;
    font-size: 13px;
}
</style>